package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IPlanMovePrdInfoService;
import com.wondertek.poms.dao.po.PlanMovePrdInfo;
import com.wondertek.poms.dao.repo.IPlanMovePrdInfoDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author evan0213
 * @description
 */
@Service
public class PlanMovePrdInfoServiceImpl extends ServiceImpl<PlanMovePrdInfo, Long> implements IPlanMovePrdInfoService {
	@Autowired
	private IPlanMovePrdInfoDao planMovePrdInfoDao;

	@Override
	public PlanMovePrdInfo deleteLogic(Long id) {
		return null;
	}

	@Override
	public PlanMovePrdInfo merge(PlanMovePrdInfo model) {
		return null;
	}

	@Override
	public Page<PlanMovePrdInfo> findAll(PlanMovePrdInfo entity, int page, int rows, String sord, String sidx) {
		Pageable pageable = getPageable(page - 1, rows, sidx, sord);
		Specification<PlanMovePrdInfo> specification = getWhereClause(entity);
		return planMovePrdInfoDao.findAll(specification, pageable);
	}

	private Specification<PlanMovePrdInfo> getWhereClause(final PlanMovePrdInfo planMovePrdInfo) {
		return (Specification<PlanMovePrdInfo>) (root, query, cb) -> {
			List<Predicate> predicate = new ArrayList<>();
			if (null != (planMovePrdInfo.getPrdContId())) {
				predicate.add(cb.equal(root.get("prdContId"), planMovePrdInfo.getPrdContId()));
			}
			if (null != (planMovePrdInfo.getContentId())) {
				predicate.add(cb.equal(root.get("contentId"), planMovePrdInfo.getContentId()));
			}
			if (null != (planMovePrdInfo.getOldPrdInfoId())) {
				predicate.add(cb.equal(root.get("oldPrdInfoId"), planMovePrdInfo.getOldPrdInfoId()));
			}
			if (null != (planMovePrdInfo.getNewPrdInfoId())) {
				predicate.add(cb.equal(root.get("newPrdInfoId"), planMovePrdInfo.getNewPrdInfoId()));
			}
			if (StringUtils.isNotBlank(planMovePrdInfo.getStatus())) {
				predicate.add(cb.equal(root.get("status"), planMovePrdInfo.getStatus()));
			}
			if (null != (planMovePrdInfo.getStartTime())) {
				predicate.add(cb.greaterThanOrEqualTo(root.get("planTime"), planMovePrdInfo.getStartTime()));
			}
			if (null != (planMovePrdInfo.getEndTime())) {
				predicate.add(cb.lessThanOrEqualTo(root.get("planTime"), planMovePrdInfo.getEndTime()));
			}
			if (StringUtils.isNotBlank(planMovePrdInfo.getCreatePerson())) {
				predicate.add(cb.like(root.get("createPerson"), planMovePrdInfo.getCreatePerson()));
			}
			Predicate[] pre = new Predicate[predicate.size()];
			return query.where(predicate.toArray(pre)).getRestriction();
		};
	}

	@Transactional
	@Override
	public int offLine(Long id) {
		return planMovePrdInfoDao.offLine(id);
	}
}
